package com.lbn.baseframe.mvp;

import android.view.View;

import com.lbn.baseframe.net.business.RxHelper;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author 李炳南
 * @date 2017/2/8
 */
public class BasePresenter<V extends IMvp> implements IPresenter<V> {


    private Subscriber subscriber;

    private V view;

    @Override
    public V getView() {
        return view;
    }

    @Override
    public void attachView(V view) {
        this.view = view;
    }

    @Override
    public void destroy() {
        if (subscriber != null && subscriber.isUnsubscribed()) {
            subscriber.unsubscribe();
        }
    }

    @Override
    public void over() {
        if (subscriber != null && subscriber.isUnsubscribed()) {
            subscriber.unsubscribe();
        }
    }

    @Override
    public void subscribeNetwork(Observable observable, Subscriber subscriber, Observable.Transformer transformer) {
        this.subscriber = subscriber;
        observable.compose(transformer)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    @Override
    public void subscribeNetwork(Observable observable, Subscriber subscriber) {
        subscribeNetwork(observable, subscriber, RxHelper.handleResult());
    }

}


